<?php

#chat.php
#
#Part of chatboss project
#
#POST the messageObject directly from the client , returns object.
#
# actionId
#	numerical ID of the action you wish to perform.
#		1: Message another user
#
# [messageSender]
#	The userID of the user who sent the message
#
# [messageRecipient]
#	The userID of the message recipient
#
# [messageText]
#	The text of the message itself
#

include 'queryengine.php';
include 'functions.php';
$outputObject = new protocolObject();

if(isset($_POST['messageObject']))
{
	$messageObject = json_decode((string)$_POST['messageObject']);
	
	if(gettype($messageObject))
	{
		if($messageObject->actionId == 1)
		{
			#post code
			if($messageObject->messageSender != $messageObject->messageRecipient)
			{
				$outputObject->messageText = $messageObject->messageText;
				$outputObject->messageSender = $messageObject->messageSender;
				$outputObject->messageRecipient = $messageObject->messageRecipient;
				$outputObject->senderQuery = "select count(*) from user where userId= '" . $messageObject->messageSender . "';";
				$outputObject->recipientQuery = "select count(*) from user where userId = '" . $messageObject->messageRecipient . "';";
				$outputObject->senderQueryResult = crowFather($outputObject->senderQuery,5);
				$outputObject->recipientQueryResult = crowFather($outputObject->recipientQuery,5);
				
				if($outputObject->senderQueryResult == 1 && $outputObject->recipientQueryResult == 1)
				{
					#both the users exist, we can send the message
					#Only continue if this is the case
					
					$cleanString = crowFather($outputObject->messageText,6);
					$outputObject->insertQuery = 'insert into message (messageSender,messageRecipient,messageText) values(' . $outputObject->messageSender . ',' . $outputObject->messageRecipient . ',' . $cleanString . ');';
					crowFather('insert into message (messageSender,messageRecipient,messageText) values(' . $outputObject->messageSender . ',' . $outputObject->messageRecipient . ',' . $cleanString . ');',3);
					$outputObject->giveExitStatus(0,"Both users exist and the message was sent");
				}
				if($outputObject->senderQueryResult == 0 || $outputObject->recipientQueryResult == 0)
				{
					#One or both users do not exist
					$outputObject->giveExitStatus(1,"The sender or recipient do not exist");
				}
				if($outputObject->senderQueryResult > 1 || $outputObject->recipientQueryResult > 1)
				{
					#One or both users have duplicate values in the user tables
					$outputObject->giveExitStatus(1,"The sender or recipient given has duplicate entries in the user table");
				}
			}
			else
			{
				#user is attempting to send to themselves
				$outputObject->giveExitStatus(1,"You are trying to send this message to yourself");
			}
		}
		if($messageObject->actionId == 2)
		{
			#get code
		}
		if($messageObject->actionId == 3)
		{
			#create user code
		}
		if($messageObject->actionId == 4)
		{
			#delete user code
		}
		if($messageObject->actionId > 4)
		{
			$outputObject->giveExitStatus(1,"The action type was not one of the supported actions");
		}
		sendDebugInfo($outputObject);
		if($outputObject->exitCode == 2)
		{
			$outputObject->giveExitStatus(1,"An exit status was not provided");
		}
		echo json_encode($outputObject);
	}
	else
	{
		$outputObject->giveExitStatus(1,"The input type is incorrect. Ensure you are using an object.");
		echo json_encode($outputObject);
	}

	
}
else
{
	$outputObject->giveExitStatus(1,"This script only takes one object input");
	echo json_encode($outputObject);
}

?>